package com.ncloud.resman.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.exception.UserException;
import com.ncloud.resman.entity.IpAddress;
import com.ncloud.resman.mapper.CabinetUseMapper;
import com.ncloud.resman.mapper.IpAddressMapper;
import com.ncloud.resman.mapper.SequenceMapper;
import com.ncloud.resman.service.IpAddressService;
@Service
public class IpAddressServiceImpl implements IpAddressService{
	@Resource
	private IpAddressMapper ipAddressMapper;
	@Resource
	private CabinetUseMapper cabinetUseMapper;
	@Resource
	private SequenceMapper sequenceMapper;

	@Override
	public JSONArray getIpAddressInfo(JSONObject json) {
		String netType = json.getString("netType");
		String ipSegment = json.getString("ipSegment");
		String state = json.getString("state");
		String ip = json.getString("ip");
		List<IpAddress> ipAddrs = ipAddressMapper.getIpAddressInfo(netType, ipSegment, state, ip);
		JSONArray results=JSON.parseArray(JSON.toJSONString(ipAddrs));
		return results;
	}

	@Override
	public int addIPSingle(IpAddress ipAddress) {
		List<JSONObject> temps = ipAddressMapper.getIp(ipAddress.getIp());
		if(temps!=null && temps.size()>0){
			throw new UserException(4003,"IP:"+ipAddress.getIp()+" 已经存在");  
		}
		Long id = sequenceMapper.getSequence("ip_address_id").longValue();
		ipAddress.setId(id);
		ipAddress.setState("0");
    	return ipAddressMapper.insertIpAddress(ipAddress);
	}
	@Override
	public int addIPRegion(Object userId,JSONObject param) {
		String netType = param.getString("netType");
		String ipSegment = param.getString("ipSegment");
		String startIp = param.getString("startIp");
		String endIp = param.getString("endIp"); 
		String[] startIps=startIp.split("\\.");
		String[] endIps=endIp.split("\\.");
		
		int startIp3=Integer.valueOf(startIps[3]);
		int endIp3=Integer.valueOf(endIps[3]);
		int insertNum=0;
		while(startIp3<=endIp3){
			String ip = startIps[0]+"."+startIps[1]+"."+startIps[2]+"."+startIp3;
			List<JSONObject> temps = ipAddressMapper.getIp(ip);
			if(temps!=null && temps.size()>0){
				throw new UserException(4003,"IP:"+ip+" 已经存在");  
			}
			Long id = sequenceMapper.getSequence("ip_address_id").longValue();
			IpAddress ipAddress = new IpAddress();
			ipAddress.setId(id);
			ipAddress.setNetType(netType);
			ipAddress.setIpSegment(ipSegment);
			ipAddress.setIp(ip);
			ipAddress.setState("0");
			ipAddress.setCreateUser(userId==null?1:Long.parseLong(userId.toString()));
	    	ipAddress.setCreateDate(new Date());
	    	insertNum=insertNum+ipAddressMapper.insertIpAddress(ipAddress);
	    	startIp3++;
		}
    	return insertNum;
	}
	@Override
	public int updateIpAddress(IpAddress ipAddress) {
		List<JSONObject> temps = ipAddressMapper.getIp(ipAddress.getIp());
		if(temps!=null && temps.size()>0&&!temps.get(0).get("id").equals(ipAddress.getId())){
			throw new UserException(4003,"IP:"+ipAddress.getIp()+" 已经存在");  
		}
		return ipAddressMapper.updateIpAddress(ipAddress);
	}

	@Override
	public int deleteIpAddress(Long id) {
		return ipAddressMapper.deleteIpAddress(id);
	}

	@Override
	public int allotIpAddress(IpAddress ipAddress) {
		return ipAddressMapper.updateIpAddressState(ipAddress);
	}
	@Override
	public int releaseIpAddress(IpAddress ipAddress) {
		return ipAddressMapper.updateIpAddressState(ipAddress);
	}
	@Override
	public JSONArray getIpSegment(String netType) {
		List<JSONObject> ipSegments = ipAddressMapper.getIpSegment(netType);
		JSONArray results=JSON.parseArray(JSON.toJSONString(ipSegments));
		return results;
	}

	@Override
	public int isIpValid(String ip) {
		int validTag=1;//0-不存在;1-可用;2-已经占用
		List<JSONObject> temps = ipAddressMapper.getIp(ip);
		if(temps!=null && temps.size()>0){
			if("1".equals(temps.get(0).get("state").toString())){
				validTag=2;
			} 
		}else{
			validTag=0;
		}
		return validTag;
	}

	@Override
	public int updateIpState(IpAddress ipAddress) {
		return ipAddressMapper.updateIpState(ipAddress);
	}
}
